from machine import Pin, PWM
from utime import sleep_us

# 创建PWM_LED对象
led_pins = [16, 17]
led_pwms = [PWM(Pin(pin, value=0), freq=1000, duty=0) for pin in led_pins]
# 占空比
duty_val = 0
direct = 0

# 呼吸灯
def led_breath(led_pwm, speed):
    global duty_val, direct
    if not 0 < speed < 1000:
        raise Exception("speed out of range")
    if not direct:
        if duty_val < 1023:
            duty_val += 1
        else:
            direct = 1
    else:
        if duty_val > 0:
            duty_val -= 1
        else:
            direct = 0
    led_pwm.duty(duty_val)
    sleep_us(1000 - speed)

# 调节LED亮度
def led_bright(led_pwm, level):
    if 1 <= level <= 16: 
        led_pwm.duty(level * 64 - 1)
    else:
        raise Exception("level out of range")

if __name__ == "__main__":
    # PWM调制呼吸灯
    while True:
        led_breath(led_pwms[0], 200)
        led_bright(led_pwms[1], 1)
    
    
    
    